//获取应用实例
const app = getApp()

Page({
    data: {
      boxId:"",
      reports:[],
      box:{},
      scoreItems:[],
      scoreItem:"",
      showScoreItemInput:false,
      score_items_p:[],
      scoreItemVisible:false,
      score_item:""||app.globalData.score_item
    },

    onLoad: function(options) {
      if (options != null){
        const boxId= options.boxId;
        const score_item= options.score_item;
        this.setData({boxId,score_item})
        wx.request({
          url: app.globalData.url+'/wechat/box_reports?boxId='+boxId+"&score_item="+score_item,
          method: 'get',
          success: res2 => {
            const reports=res2.data;
            console.log('reports',reports)
            this.setData({reports});
            wx.setNavigationBarTitle({
              title: score_item
          })
          }
        })
      }
    },

    showDialog: function(){
      this.setData({showScoreItemInput:true})
    },
    closeDialog: function(){
      this.setData({showScoreItemInput:false})
    },
    creatScoreItem: function(){
      var that = this;
    
      if (this.data.scoreItem == "") {
        wx.showToast({
          title: '请输入作业项目名称',
          icon: 'none',
          duration: 2000
        })
      }else{
        const scoreItems=this.data.scoreItems;
        scoreItems.push(this.data.scoreItem);
        wx.request({
          url: app.globalData.url+'/wechat/update_report_box',
          data: {
            key:"scoreItems",
            id: that.data.boxId,
            value: scoreItems,
            token:app.globalData.token
          },
          method: 'POST',
          header: {
            'content-type': 'application/json'
          },
          success: res2 => {
              this.setData({showScoreItemInput:false,
                box:res2.data,
                scoreItems:res2.data.scoreItems})
          }
        })
      }
    },

    removeScoreItem:function(e){
      const scoreItems=this.data.scoreItems;
      const scoreItem=e.target.id
      const _scoreItems=scoreItems.filter(s=>s!==scoreItem);
      const that=this;
      wx.request({
        url: app.globalData.url+'/wechat/update_report_box',
        data: {
          key:"scoreItems",
          id: that.data.boxId,
          value: _scoreItems,
          token:app.globalData.token
        },
        method: 'POST',
        header: {
          'content-type': 'application/json'
        },
        success: res2 => {
          this.setData({scoreItems:_scoreItems});
        }
      })

    },

    cellClick:function(e){
      console.log('cellClick',e)
    },


    changeScoreItem:function(e){
      this.setData({
        scoreItem: e.detail.value
      })
    },

    report_comment:function(e){
      const reportId=e.target.id;
          wx.navigateTo({
            url: "../report_comment_web/report_comment?reportId="+reportId+"&boxId="+this.data.boxId+"&type=text"
          });
    },

    showBackModal:function(e){
      const reportId=e.target.id;
      wx.showModal({
        title: '提示',
        content: '确定退回该报告？',
        success: (res) => {
          if (res.confirm) {
            this.back_report(reportId)
          }
        }
      })
    },

    back_report:function(reportId){
      const that =this;
      wx.request({
        url: app.globalData.url+'/wechat/back_report?reportId='+reportId,
        method: 'get',
        success: res => {
          const _reports=that.data.reports;
          const reports=_reports.filter(r=>r.objectId!==reportId);
          this.setData({reports})
        }
      })
    }

     

})

